package com.doublethread.easymart.modules.order.controller;

import com.doublethread.easymart.common.result.PageResult;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.order.dto.*;
import com.doublethread.easymart.modules.order.service.OrderService;
import com.doublethread.easymart.modules.order.vo.OrderStatisticsVO;
import com.doublethread.easymart.modules.order.vo.OrderVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 管理端订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/orders")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取订单列表
     */
    @GetMapping
    public Result<PageResult<OrderVO>> getOrderList(OrderQueryDTO queryDTO) {
        log.info("管理端查询订单列表");

        PageResult<OrderVO> pageResult = orderService.getAdminOrderList(queryDTO);

        log.info("管理端查询订单列表成功");

        return Result.success(pageResult);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{id}")
    public Result<OrderVO> getOrderDetail(@PathVariable Long id) {
        log.info("管理端获取订单详情");

        OrderVO orderVO = orderService.getAdminOrderById(id);

        log.info("管理端获取订单详情成功");
        return Result.success(orderVO);
    }

    /**
     * 订单发货
     */
    @PutMapping("/{id}/delivery")
    public Result<Void> deliverOrder(@PathVariable Long id,
                                     @Valid @RequestBody OrderDeliveryDTO deliveryDTO) {
        log.info("订单发货");

        orderService.deliverOrder(id, deliveryDTO);

        log.info("订单发货成功");
        return Result.success();
    }

    /**
     * 取消订单
     */
    @PutMapping("/{id}/cancel")
    public Result<Void> cancelOrder(@PathVariable Long id,
                                    @Valid @RequestBody OrderCancelDTO cancelDTO) {
        log.info("取消订单");

        orderService.adminCancelOrder(id, cancelDTO);

        log.info("订单取消成功");
        return Result.success();
    }

    /**
     * 更新订单备注
     */
    @PutMapping("/{id}/remark")
    public Result<Void> updateOrderRemark(@PathVariable Long id,
                                          @Valid @RequestBody OrderRemarkDTO remarkDTO) {
        log.info("更新订单备注");

        orderService.updateOrderRemark(id, remarkDTO);

        log.info("订单备注更新成功");
        return Result.success();
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateOrderStatus(@PathVariable Long id,
                                          @Valid @RequestBody OrderUpdateStatusDTO statusDTO) {
        log.info("更新订单状态: id={}, status={}", id, statusDTO.getStatus());

        orderService.updateOrderStatus(id, statusDTO);

        log.info("订单状态更新成功: id={}, status={}", id, statusDTO.getStatus());
        return Result.success();
    }

    /**
     * 获取订单统计信息
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getOrderStatistics(
            @RequestParam(required = false) LocalDate startDate,
            @RequestParam(required = false) LocalDate endDate) {
        log.info("获取订单统计信息: startDate={}, endDate={}", startDate, endDate);

        OrderStatisticsVO statisticsVO = orderService.getOrderStatistics(startDate, endDate);

        log.info("获取订单统计信息成功");
        return Result.success(statisticsVO);
    }
}
